package org.lookouster.nextAgadir.pointageBureau.ui;

import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

import org.gui.JCalendarCombo;
import org.lookouster.nextAgadir.pointageBureau.outils.BDFunctions;

public class PeriodChooser extends JFrame
{
	JPanel panel1;
	
	JLabel label2;
	JLabel label3;
	
	JCalendarCombo dateDebut;
	JCalendarCombo dateFin;
	JButton okay;
	JButton cancel;
	List<Period> periodDB;
	
	private int mode;
	//mode indicate whether the Period chooser is called for:
	//1-> Establish Planning
	//2-> ???
	public PeriodChooser(int mod)
	{
		this.mode=mod;
		panel1 = new JPanel();
		
		label2 = new JLabel(UICommons.fPeriodDbut);
		label3 = new JLabel(UICommons.fPeriodEnd);
		
		dateDebut = new JCalendarCombo(JCalendarCombo.MONDAY, true, 2008, 3000, false);
		dateFin = new JCalendarCombo(JCalendarCombo.MONDAY, true, 2008, 3000, false);
		
		dateDebut.setDateFormat(JCalendarCombo.YEAR_BIG, 
				JCalendarCombo.MONTH, JCalendarCombo.DAY, '-');
		dateFin.setDateFormat(JCalendarCombo.YEAR_BIG, 
				JCalendarCombo.MONTH, JCalendarCombo.DAY, '-');
		
		Calendar calendar = Calendar.getInstance();
		calendar=resetMonthIn(calendar);
		
		dateDebut.setDate(calendar);
		dateFin.setDate(calendar);
		

		
		okay = new JButton("OK");
		cancel = new JButton("Cancel");
		panel1.setLayout(new GridLayout(3,2,5,5));
		getContentPane().setLayout(new GridLayout(1,1));
		panel1.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		panel1.add(label2);
		panel1.add(dateDebut);
		panel1.add(label3);
		panel1.add(dateFin);
		panel1.add(okay);
		panel1.add(cancel);
		getContentPane().add(panel1);
		pack();
		setTitle(UICommons.fPeriodValid);		
		setVisible(true);
		
		okay.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				//System.out.print("Mois: "+new );
				launchConcernedDialog();
			}
			
		});
		
		cancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				close();
			}
		});				

		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				close();
			}
		});
		
		periodDB=BDFunctions.getEmpsPeriodList();
	
	}
	
	public void close()
	{
		this.setVisible(false);
	}
	
	
	public static void main(String args[]) throws Exception
	{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		PeriodChooser test = new PeriodChooser(0);
	}
	
	public void launchConcernedDialog()
	{
		if(!dateCheckIsOk())
			return;
		if(mode==1)
		{
			//this.setVisible(false);
			new EtabPlanning((Frame)(this.getParent()),true, 
					new java.sql.Date(getDateDebut().getTime()),
					new java.sql.Date(getDateFin().getTime())).setVisible(true);
		}
		else
		{
			//new AjouterControle((Frame)(this.getParent()),true).setVisible(true);
			
		}
		
		this.setVisible(false);
			
	}
	
	public Date getDateDebut()
	{
		Calendar c=dateDebut.getDate();
		return resetMonthOut(c).getTime();
	}
	
	public Date getDateFin()
	{
		Calendar c=dateFin.getDate();
		return resetMonthOut(c).getTime();
	}
	
	boolean dateCheckIsOk()
	{
		//System.out.println("mDateDeb: "+getDateDebut().getTime());
		//System.out.println("DateNow: "+new Date().getTime());
		//System.out.println("mDateFin: "+getDateFin().getTime());
		if(getDateDebut().after(getDateFin()))
		{
			JOptionPane.showMessageDialog(this,UICommons.fPChooser1);
			return false;
		}
		if(getDateDebut().getTime()<new Date().getTime())
		{
			JOptionPane.showMessageDialog(this,UICommons.fPChooser2);
			return false;
		}
		
		//Check Imbriquer
		if(periodDB==null)//No period yet, we can plan the first one
			return true;
		for(Period p:periodDB)
			if(!(getDateDebut().after(p.fin) || getDateFin().before(p.debut)))
			{
				JOptionPane.showMessageDialog(this,UICommons.fPChooser3);
				return false;
			}
		return true;
		
	}
	
	
	/**
	 * Due to the month-1 incompatibility with the downloaded JComboCalendar Components
	 * @param c
	 * @return
	 */
	Calendar resetMonthOut(Calendar c)
	{
		int mois=c.get(Calendar.MONTH);
		c.set(Calendar.MONTH, mois-1);
		return c;
	}
	
	
	/**
	 * Due to the month-1 incompatibility with the downloaded JComboCalendar Components
	 * @param c
	 * @return
	 */
	Calendar resetMonthIn(Calendar c)
	{
		int mois=c.get(Calendar.MONTH);
		c.set(Calendar.MONTH, mois+1);
		return c;
	}
}	
